# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2022/7/27 14:58
# @Author : xyjiang1216
# @Email : jiangxuyan@nudt.edu.cn
# @File : mac_tbl_structure.py
# @Version: OpenTSN_planner_builder 1.0

import ctypes

from tsn_builder_struct.table_length import MAC_TBL_LEN

c_uint32 = ctypes.c_uint32


class MacEntry_bits(ctypes.BigEndianStructure):
    _fields_ = [
        # entry1
        ('valid', c_uint32, 1),
        ('reserve1', c_uint32, 14),
        ('outport1', c_uint32, 17),
        # entry2
        ('outport2', c_uint32, 16),
        ('dmac_1', c_uint32, 16),
        # entry3
        ('dmac_2', c_uint32, 32),
        #entry4
        ('reserve2', c_uint32, 32)
    ]


class MacEntry_bytes(ctypes.BigEndianStructure):
    _fields_ = [
        ('entry1', c_uint32, 32),
        ('entry2', c_uint32, 32),
        ('entry3', c_uint32, 32),
        ('entry4', c_uint32, 32)
    ]


class MacEntry_union(ctypes.Union):
    _fields_ = [
        ('bit', MacEntry_bits),
        ('byte', MacEntry_bytes)
    ]


class MacTblEachNode:
    def __init__(self,
                 node_id: int,
                 broadcast_flag):
        self.node_id = node_id
        self.broadcast_flag = broadcast_flag
        self.tbl_len = MAC_TBL_LEN
        self.tbl_each_node = []

    def append_entry(self,
                     entry: MacEntry_union):
        if len(self.tbl_each_node) <= self.tbl_len:
            self.tbl_each_node.append(entry)
        else:
            print('ERROR - inject tbl exceeds %d at node %d' % (self.tbl_len, self.node_id))
            exit(0)


class MacTbl:
    def __init__(self):
        self.tbl_set = []

    def append_tbl(self,
                   tbl: MacTblEachNode):
        self.tbl_set.append(tbl)
